package com.pikaqiu.service.impl;

import com.pikaqiu.common.ServerResponse;
import com.pikaqiu.dao.CategoryMapper;
import com.pikaqiu.pojo.Category;
import com.pikaqiu.service.ICategoryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2018/6/11.
 */
@Service
public class CategoryServiceImpl implements ICategoryService{

    @Autowired
    private CategoryMapper categoryMapper;

    Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Override
    public ServerResponse addCategory(String categoryName, Integer parentId) {

        if (parentId == null || StringUtils.isBlank(categoryName)) {
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Category category = new Category();

        category.setName(categoryName);

        category.setParentId(parentId);

        category.setStatus(true);

        int count = categoryMapper.insert(category);

        if (count > 0) {
            return ServerResponse.createBySuccessMessage("添加品类成功");
        }
        return ServerResponse.createByErrorMessage("添加品类失败");
    }

    @Override
    public ServerResponse updateCategoryName(Integer categoryId, String categoryName) {

        if (categoryId == null || StringUtils.isBlank(categoryName)) {
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Category category = new Category();
        category.setId(categoryId);
        category.setName(categoryName);
        int count = categoryMapper.updateByPrimaryKeySelective(category);
        if (count > 0) {
            return ServerResponse.createBySuccessMessage("修改品类成功");
        }
        return ServerResponse.createByErrorMessage("修改品类失败");
    }

    /**
     * 查询子节点的信息 但是不递归
     *
     * @param categoryId
     * @return
     */
    @Override
    public ServerResponse<List<Category>> ChildrenParallel(Integer categoryId) {
        List<Category> list = categoryMapper.ChildrenParallel(categoryId);
        if (CollectionUtils.isEmpty(list)) {
            logger.warn("未找到对应的子节点");
        }
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse<Set<Category>> AllChildrenParallel(Set<Category> set, Integer categoryId) {
        Category categoryP = categoryMapper.selectByPrimaryKey(categoryId);
        //找到子节点
        List<Category> list = categoryMapper.ChildrenParallel(categoryId);
        //不为空继续查找子节点的子节点
        if (!CollectionUtils.isEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                //递归查询
                Category category = list.get(i);
                ServerResponse<Set<Category>> response = AllChildrenParallel(set,category.getId());
                if (response.getData().size() > 0) {
                    //放入返回集合
                    set.addAll(response.getData());
                }
            }
        }
        if (null != categoryP && !set.contains(categoryP)) {
            set.add(categoryP);
        }
        return ServerResponse.createBySuccess(set);
    }

}
